import { _goodsList } from "../../service/api";

Component({
  properties: {
    goodsList: {
      type: Array,
      value: [],
    },
    isTab: {
      type: Boolean,
      value: true,
    },
    isScroll: {
      type: Boolean,
      value: true,
    },
    count: {
      type: Number,
      value: 0,
    },
  },
  data: {
    page: 1,
    size: 20,
    currentSortType: "default", // 排序的类型
    saleSortOrder: "desc", // 销量排序的方式
    priceSortOrder: "desc", // 价格排序的方式
    goodsList: [],
  },
  attached() {
    // this.getGoodsList();
    console.log(this.data);
  },
  methods: {
    async getGoodsList(order = "desc") {
      const { page, size, currentSortType, isHot, isNew } = this.data;
      const res = await _goodsList({ isHot, isNew, page, size, order, sort: currentSortType });
      this.setData({ goodsList: res.data });
    },
    // 排序
    openSortFilter(e) {
      let { priceSortOrder, saleSortOrder } = this.data;
      const curentId = e.currentTarget.id;
      switch (curentId) {
        case "saleSort":
          this.setData({
            saleSortOrder: saleSortOrder == "asc" ? "desc" : "asc",
            currentSortType: "sale",
          });
          this.triggerEvent("changeSort", { type: "sale", order: saleSortOrder });
          break;
        case "priceSort":
          this.setData({
            priceSortOrder: priceSortOrder == "asc" ? "desc" : "asc",
            currentSortType: "price",
          });
          this.triggerEvent("changeSort", { type: "price", order: priceSortOrder });
          break;
        default:
          this.setData({ currentSortType: "default" });
          this.triggerEvent("changeSort", { type: "default", order: "desc" });
      }
    },
    // 加载更多
    handleReachBottom() {
      this.setData({ page: parseInt(this.data.page + 1) });
      const { page, size, currentSortType, priceSortOrder, saleSortOrder, count } = this.data;
      let order;
      switch (currentSortType) {
        case "price":
          order = priceSortOrder;
          break;
        case "sale":
          order = saleSortOrder;
          break;
        default:
          order = "desc";
      }
      if (page <= Math.ceil(count / size)) {
        this.triggerEvent("reachBottom", { page, size, type: currentSortType, order });
      }
    },
  },
});
